#include <QDebug>
#include "cmanager.h"

CManager::CManager() {

}

/** 加载库
 * @brief CManager::loadLib
 * @return
 */
bool CManager::loadLib()
{
    // 加载so库
    m_plib = new QLibrary("../lib/libEEGAnalyse.so");

    if(m_plib->load() == false)
    {
        qDebug() << "libEEGAnalyse.so load error, please check";
        delete m_plib;
        m_plib = nullptr;
        return false;
    }


    qDebug() << "libEEGAnalyse.so load Success";

    // 解析 EEGAnalyse_InitParam 函数
    m_func_InitParam = reinterpret_cast<EEGAnalyse_InitParam>(m_plib->resolve("EEGAnalyse_InitParam"));
    if (m_func_InitParam == nullptr)
    {
        qDebug() << "EEGAnalyse_InitParam Error";
        return false;
    }
    else
    {
        qDebug() << "EEGAnalyse_InitParam Success";
    }

    // 获取 EEGAnalyse_InputData 函数
    m_func_InputData = reinterpret_cast<EEGAnalyse_InputData>(m_plib->resolve("EEGAnalyse_InputData"));
    if(m_func_InputData == nullptr)
    {
        qDebug() << "EEGAnalyse_InputData Error";
        return false;
    }
    else
    {
        qDebug() << "EEGAnalyse_InputData Success";
    }

    // 获取EEGAnalyse_GetAmpResult函数
    m_func_GetAmpResult = reinterpret_cast<EEGAnalyse_GetAmpResult>(m_plib->resolve("EEGAnalyse_GetAmpResult"));
    if (m_func_GetAmpResult == nullptr)
    {
        qDebug() << "EEGAnalyse_GetAmpResult Error";
        return false;
    }
    else
    {
        qDebug() << "EEGAnalyse_GetAmpResult Success";
    }

    // 获取 EEGAnalyse_GetEEGResult
    m_func_GetEEGResult = reinterpret_cast<EEGAnalyse_GetEEGResult>(m_plib->resolve("EEGAnalyse_GetEEGResult"));
    if (m_func_GetEEGResult == nullptr)
    {
        qDebug() << "EEGAnalyse_GetEEGResult Error";
        return false;
    }
    else
    {
        qDebug() << "EEGAnalyse_GetEEGResult Success";
    }

    return true;

}

/** 打开数据文件
 * @brief CManager::openDataFile
 * @return
 */
bool CManager::openDataFile()
{
    bool bRet = false;

    m_pFile = new QFile("../Data/EEG.Data");

    if(m_pFile->open(QIODevice::ReadOnly) == false)
    {
        qDebug() << "open data file error";
        delete m_pFile;
        m_pFile = nullptr;
        return bRet;
    }

    qDebug() << "open data file success";
    bRet = true;
    return bRet;

}

/** 开始分析脑电数据
 * @brief CManager::analyse
 * @return
 */
bool CManager::analyse()
{
    if (m_func_InitParam == nullptr || m_func_InputData == nullptr || m_func_GetAmpResult == nullptr || m_func_GetEEGResult == nullptr)
    {
        qDebug() << "library interface error, please check";
        return false;
    }

    if (m_pFile == nullptr)
    {
        qDebug() << "data file error, please check";
        return false;
    }

    int iChanNum = 32;      // 32通道
    int iFrequecy = 256;    // 采样率256Hz

    double* pData = new double[iChanNum];
    memset(pData,0,sizeof(double) * iChanNum);


    m_pAmpResult = new tagAmpCalRet[iChanNum]();
    m_pEEGResult = new tagEEGCalRet[iChanNum]();

    int iFrameSize = sizeof(double) * iChanNum;
    int iFrameNum = m_pFile->size() / iFrameSize;

    int iEEGCalLen = iFrequecy * 8;
    int iAmpCalLen = iFrequecy * 6;
    m_pFile->seek(0);


    m_func_InitParam(iChanNum, iFrequecy);

    qDebug() << "analyse begin:";

    for (int iIndex = 0; iIndex < iFrameNum; ++iIndex)
    {
        int iReadLen = iFrameSize;
        m_pFile->read((char*)pData, iReadLen);

        m_func_InputData(pData);

        if ((iIndex % iEEGCalLen == 0) && (iIndex != 0))
        {
            m_func_GetEEGResult(m_pEEGResult);
            qDebug("Ratio1:%0.2f Ratio2:%0.2f Ratio3:%0.2f Ratio4:%0.2f Ratio5:%0.2f Total:%f",
                   m_pEEGResult[0].fRaio[0],
                   m_pEEGResult[0].fRaio[1],
                   m_pEEGResult[0].fRaio[2],
                   m_pEEGResult[0].fRaio[3],
                   m_pEEGResult[0].fRaio[4],
                   m_pEEGResult[0].dbTotalEnergy);
        }
    }

}
